package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.TopoEdge;
import com.ruoyi.system.domain.vo.TopoVO;
import com.ruoyi.system.mapper.TopoEdgeMapper;
import com.ruoyi.system.service.ITopoEdgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TopoEdgeServiceImpl implements ITopoEdgeService {

    @Autowired
    private TopoEdgeMapper topoEdgeMapper;

    public Map<String, ArrayList<TopoEdge>> get_update_add_delete_list(List<TopoEdge> newTopoEdges, List<TopoEdge> originTopoEdges,Long topoVoId) {
        ArrayList<TopoEdge> update_list = new ArrayList<>();
        ArrayList<TopoEdge> add_list = new ArrayList<>();
        ArrayList<TopoEdge> delete_list = new ArrayList<>();
        Map<String, ArrayList<TopoEdge>> result = new HashMap<>();

        //寻找需要新增的,遍历前端传过来的数组和数据库中已有的数据比较，如果前端传过来的元素在数据库中找不到的话，那么新增它
        for (TopoEdge newTopoEdge : newTopoEdges) {
            Boolean flag = true;
            for (TopoEdge originTopoEdge : originTopoEdges) {
                if (Objects.equals(newTopoEdge.getEdgeId(), originTopoEdge.getEdgeId())) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                newTopoEdge.setTopoId(topoVoId);
                add_list.add(newTopoEdge);
            }
        }

        //寻找需要更新的元素，遍历前端传过来的数组和数据库中已有的数据比较，如果前端传过来的数组数据库中已经有了，那么更新它
        for (TopoEdge newTopoEdge : newTopoEdges) {
            Boolean flag = false;
            for (TopoEdge originTopoEdge : originTopoEdges) {
                if (Objects.equals(newTopoEdge.getEdgeId(), originTopoEdge.getEdgeId())) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                newTopoEdge.setTopoId(topoVoId);
                update_list.add(newTopoEdge);
            }
        }

        //寻找需要删除的数据，就是前端传过来的数据里没有但是原来数据库中已经存在的数据
        for (TopoEdge originTopoEdge : originTopoEdges) {
            Boolean flag = true;
            for (TopoEdge newTopoEdge : newTopoEdges) {
                if (Objects.equals(newTopoEdge.getEdgeId(), originTopoEdge.getEdgeId())) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                delete_list.add(originTopoEdge);
            }
        }
        result.put("add", add_list);
        result.put("update", update_list);
        result.put("delete", delete_list);
        return result;
    }

    @Override
    public void addTopoEdge(TopoVO topoVO) {
        List<TopoEdge> newTopoEdges = topoVO.getEdges();
        List<TopoEdge> originTopoEdges = topoEdgeMapper.selectByTopoId(topoVO.getTopoId());
        Map<String, ArrayList<TopoEdge>> result = get_update_add_delete_list(newTopoEdges, originTopoEdges,topoVO.getTopoId());
        ArrayList<TopoEdge> updateList = result.get("update");
        ArrayList<TopoEdge> addList = result.get("add");
        ArrayList<TopoEdge> deleteList = result.get("delete");

        Long[]deleteids = new Long[deleteList.size()];
        for(int i = 0;i<deleteList.size();i++){
            deleteids[i]=deleteList.get(i).getEdgeId();
        }
        if (!updateList.isEmpty()) {
            //更新
            for (TopoEdge topoEdge : updateList) {
                topoEdgeMapper.updateTopoEdges(topoEdge);
            }
        }
        if (deleteids.length>0) {
            //删除
            topoEdgeMapper.deleteByTopEdgeIds(deleteids);
        }
        if (!addList.isEmpty()) {
            //新增
            topoEdgeMapper.addTopoEdges(addList);
        }


    }
}
